/****************************************************************************
   Copyright 2013 Stacy Doss

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
****************************************************************************/

#include "Def.h"

#include <iostream>
#include <stdio.h>

//extern "C" int yylex();
extern "C" int yyparse();
extern "C" FILE *yyin;

extern def::Def* root;

//// ************************************************************************
namespace def {
	Def::Def() {
		_dieArea 	= new util::Rect();
		_tracks 	= new std::vector<def::Track*>();
		_gCellGrids = new std::vector<def::GCellGrid*>();
		_components = new std::vector<def::Component*>();
		_pins 		= new std::vector<def::Pin*>();
		_nets 		= new std::vector<def::Net*>();
	};

	Def::~Def() {
	};


	int
	Def::addAttribute(std::string name, char* value) {
		_stringAttributes.insert(std::pair<std::string, std::string>(name, std::string(value)));
		return 1;
	};


	int
	Def::addAttribute(std::string name, std::string value) {
		_stringAttributes.insert(std::pair<std::string, std::string>(name, value));
		return 1;
	};


	int
	Def::addAttribute(std::string name, int value) {
		_intAttributes.insert(std::pair<std::string, int>(name, value));
		return 1;
	};


	int
	Def::addAttribute(std::string name, double value) {
		_floatAttributes.insert(std::pair<std::string, double>(name, value));
		return 1;
	};


	int
	Def::addAttribute(std::string name, bool value) {
		_boolAttributes.insert(std::pair<std::string, bool>(name, value));
		return 1;
	};


	std::string
	Def::getStringAttribute(std::string name) {
		return _stringAttributes[name];
	};


	int
	Def::getIntAttribute(std::string name) {
		return _intAttributes[name];
	};


	double
	Def::getFloatAttribute(std::string name) {
		return _floatAttributes[name];
	};


	bool
	Def::getBoolAttribute(std::string name) {
		return _boolAttributes[name];
	};


	int
	Def::load(char * filename) {

		root = this;

		// open a file handle to a particular file:
		FILE *myfile = fopen(filename, "r");
		// make sure it is valid:
		if (!myfile) {
			std::cout << "I can't open a.snazzle.file!" << std::endl;
			return -1;
		}
		// set flex to read from it instead of defaulting to STDIN:
		yyin = myfile;

		// parse through the input until there is no more:
		do {
			yyparse();
		} while (!feof(yyin));

		printf("Finished\n");
	}

	void Def::setDieArea(util::Rect*) {

	};

	void Def::setDieArea(util::Point* origin, util::Point* extent) {
		_dieArea->setOrigin(origin);
		_dieArea->setOrigin(extent);
	};

	void Def::addTrack(def::Track* track) {
		_tracks->push_back(track);
	}

	void Def::addGCellGrid(def::GCellGrid* gCellGrid) {
		_gCellGrids->push_back(gCellGrid);
	}

	void Def::addComponent(def::Component* component) {
		_components->push_back(component);
	}

	void Def::addPin(def::Pin* pin) {
		_pins->push_back(pin);
	}

	void Def::addNet(def::Net* net) {
		_nets->push_back(net);
	}

	std::vector<def::Pin*>*
	Def::getPinVector() {
		return _pins;
	}

};
